import { defineStore } from "pinia";
import type { Worknum } from "@/types/worknum";
import { getworknum, getecharts, getnav, getworktable, delworktable } from "@/api/HomeAPI";
import type { Eachts } from '@/types/Eachts'
import type { WorkData } from './../types/WorkData';
type States = {
    //住户房屋车位设备总数量
    worknum: Partial<Worknum>;
    Eachts: Partial<Eachts>;
    navEach: Partial<any[]>;
    //获取工作表格数据
    workDatas: Partial<WorkData[]>;
    searchtitle: string;
    searchStr: string;
    total: number;
    // 分页
    page: number;
    limit: number;
};
type Getters = {};
type Actions = {
    getworknums: () => Promise<void>;
    getecharts: () => Promise<void>;
    getnavEach: () => Promise<void>;
    getworktable: () => Promise<void>;
    delworktable: (id: number) => Promise<void>;
};
export const useHomeStore = defineStore<"Homes", States, Getters, Actions>(
    "Homes",
    {
        // 当前 store 中要保存的状态
        state: () => ({
            worknum: {},
            Eachts: {},
            navEach: [],
            workDatas: [],
            searchtitle: 'name',
            searchStr: '',
            total: 0,
            page: 1,
            limit: 10,
        }),
        // pinia 中的计算属性, 根据当前状态计算新状态
        getters: {},
        // 执行更改状态的逻辑
        actions: {
            // 获取工号
            async getworknums() {
                try {
                    const res = await getworknum();
                    this.worknum = res.data;
                } catch (err) {
                    console.log(err);
                }
            },
            // 获取echarts
            async getecharts() {
                try {
                    const res = await getecharts();
                    this.Eachts = res.data;
                } catch (err) {
                    console.log(err);
                }
            },
            //获取naveach
            async getnavEach() {
                try {
                    const res = await getnav();
                    this.navEach = res.data;
                } catch (err) {
                    console.log(err);
                }
            },
            //获取工作表格数据
            async getworktable() {
                try {
                    const res = await getworktable(this.searchtitle, this.searchStr, this.page, this.limit);
                    this.workDatas = res.data;
                } catch (err) {
                    console.log(err);
                }
            },
            //删除工作表格数据
            async delworktable(id: number) {
                try {
                    const res = await delworktable(id);
                    this.getworktable()
                } catch (err) {
                    console.log(err);
                }
            }
        },
        // 将当前 store 中的状态持久化到 localStorage 中
        persist: true,
    }
);